package com.ycsj.goldmedalnewconcept.activity;

import io.rong.imkit.RongIM;
import io.rong.imlib.model.UserInfo;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;

import se.emilsjolander.stickylistheaders.StickyListHeadersAdapter;
import se.emilsjolander.stickylistheaders.StickyListHeadersListView;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.FormEncodingBuilder;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import com.squareup.picasso.Picasso;
import com.ycsj.goldmedalnewconcept.bean.GldConversationGroupInfo;
import com.ycsj.goldmedalnewconcept.bean.GldConversationGroupInfo.GrouplistDetail;
import com.ycsj.goldmedalnewconcept.constant.ConfigConstant;
import com.ycsj.goldmedalnewconcept.constant.Constant;
import com.ycsj.goldmedalnewconcept.utils.SPUtil;
import com.ycsj.goldmedalnewconcept.utils.SPUtils;
import com.ycsj.goldmedalnewconcept.R;
//群资料
public class GroupConversationInfoActivity extends BaseJpStarActivity {
	private TextView textView1;
	private TextView textView2;
	private StickyListHeadersListView lv_group_list;
	private ImageView imageView2;
	public ArrayList<GrouplistDetail> list = new ArrayList<GrouplistDetail>();
	public ArrayList<UserInfo> UserList = new ArrayList<UserInfo>();
	private TextView tv_title_main;
	private TextView tv_main_back;
	private ArrayList<String> listurl;

	@Override
	protected void onCreate(Bundle arg0) {
		// TODO Auto-generated method stub
		super.onCreate(arg0);

		setContentView(R.layout.activity_groupconversitioninfo);

		tv_main_back = (TextView) findViewById(R.id.tv_main_back);
		tv_title_main = (TextView) findViewById(R.id.tv_title_main);
		textView1 = (TextView) findViewById(R.id.textView1);
		textView2 = (TextView) findViewById(R.id.textView2);
		imageView2 = (ImageView) findViewById(R.id.imageView2);

		lv_group_list = (StickyListHeadersListView) findViewById(R.id.lv_group_list);

		tv_title_main.setText("群资料");

//		String path = "http://139.129.57.83:8080/im/friend/GetGroupMemberList.aspx";
		
//		OkHttpClient mOkHttpClient = new OkHttpClient();
//		
//		mOkHttpClient.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
//		FormEncodingBuilder builder = new FormEncodingBuilder();
//		String stringExtra = getIntent().getStringExtra("GroupId");
//		builder.add("groupid",stringExtra);
//		Log.e("home", "groupiii="+stringExtra);
//		Request request = new Request.Builder()
//		.url(path).post(builder.build()).build();
//		mOkHttpClient.newCall(request).enqueue(callBack);
		OkHttpClient mOkHttpClient = new OkHttpClient();
//		mOkHttpClient.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
//		String GroupId = getIntent().getStringExtra("GroupId");
		
		FormEncodingBuilder builder = new FormEncodingBuilder();
		builder.add("type", "24");
		builder.add("c",  SPUtil.getString(GroupConversationInfoActivity.this, "account", ""));
		builder.add("role", SPUtil.getString(GroupConversationInfoActivity.this, "role", ""));
		builder.add("groupId", SPUtils.getString(GroupConversationInfoActivity.this, "clickGroupid"));

		
		//		builder.add("c",  "15333838051");
//		builder.add("role",  "1");
//		builder.add("groupId", "1533383805121");//?type=24&c=13006314760&role=3&groupId=1300631476021
		Request request = new Request.Builder()
		.url(Constant.ycsj_count_MessageCenter)
//		.url("http://pm.moregolden.com/ycsj_count/MessageCenter")
		.post(builder.build()).build();
mOkHttpClient.newCall(request).enqueue(callBack);
		tv_main_back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();

			}
		});

		lv_group_list.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {

				String userId = list.get(position).USER_ID;
				if (userId.equals(SPUtils.getString(
						GroupConversationInfoActivity.this,
						ConfigConstant.USERNAME))) {
					SPUtils.put(GroupConversationInfoActivity.this,
							"onUserPortraitClickid", SPUtils.getString(
									GroupConversationInfoActivity.this,
									ConfigConstant.USERNAME));
					
					
//					Intent intent = new Intent(
//							GroupConversationInfoActivity.this,
//							MineConversationInfo.class);
//					intent.putExtra("userInfo", SPUtils.getString(
//							GroupConversationInfoActivity.this,
//							ConfigConstant.USERNAME));
//					SPUtil.putString(GroupConversationInfoActivity.this, "clickRole", list.get(position).ROLE);
//					
//					Log.e("clickRole", list.get(position).ROLE);
//					startActivity(intent);
					if(SPUtils.getString(
							GroupConversationInfoActivity.this,
							ConfigConstant.USERNAME).length()==11){
						
					
					Intent intent = new Intent(GroupConversationInfoActivity.this,UserInfoActivity.class);
					intent.putExtra("c", SPUtils.getString(
							GroupConversationInfoActivity.this,
							ConfigConstant.USERNAME));
					intent.putExtra("role",list.get(position).ROLE);
					startActivity(intent);
					}
					
					
					
					
				} else {

					if (RongIM.getInstance() != null)

						RongIM.getInstance().startPrivateChat(
								GroupConversationInfoActivity.this,
								list.get(position).USER_ID+list.get(position).ROLE,
								list.get(position).NAME);
//					RongIM.getInstance().setCurrentUserInfo(UserList.get(position));
					SPUtils.put(GroupConversationInfoActivity.this,
							"tagetUsername", list.get(position).NAME);
					SPUtils.put(GroupConversationInfoActivity.this,
							"onUserPortraitClickid", list.get(position).USER_ID);
				}
			}
		});

		imageView2.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				// Toast.makeText(MineConversationInfo.this, "else", 0).show();

//				Intent intent3 = new Intent(GroupConversationInfoActivity.this,
//						GetPicShowActivity.class);
//				intent3.putStringArrayListExtra("urllist", listurl);
//				intent3.putExtra("position", 0);
//				startActivity(intent3);
			}
		});
	}
	Handler handler=new Handler();
Callback callBack=new Callback() {
	
	@Override
	public void onResponse(Response arg0) throws IOException {
		String json = arg0.body().string();
		// 使用Gson把json解析成对象
		// 1 得到gson对象

		Log.i("home", json);

		Gson gson = new Gson();
		// // 2 使用gson对象的方法
		final GldConversationGroupInfo mConversationGroupInfo = gson.fromJson(json,
				GldConversationGroupInfo.class);
//		Log.i("home", "mConversationG" + mConversationGroupInfo.groupid);
//		Log.i("home", mConversationGroupInfo.groupid);
		handler.postDelayed(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				
		
		textView1.setText(SPUtils.getString(GroupConversationInfoActivity.this, "clickGroupname"));
		textView2.setText(SPUtils.getString(GroupConversationInfoActivity.this, "clickGroupid"));
		// (String member_status, String member_type,
		// String user_headimg, String user_name, String user_type,
		// String userid)
		listurl = new ArrayList<String>();
//		listurl.add(mConversationGroupInfo.group_headimg);
//		list.add(new ConversationGroupListInfo(
//				mConversationGroupInfo.member_status,
//				mConversationGroupInfo.member_type,
//				mConversationGroupInfo.user_headimg,
//				mConversationGroupInfo.user_name,
//				mConversationGroupInfo.user_type,
//				mConversationGroupInfo.userid));
		list.addAll(mConversationGroupInfo.list);
		
		
    	Collections.reverse(list);//倒序处理
//		for (GrouplistDetail i : list) {
//			UserList.add(new UserInfo(i.userid, i.user_name, Uri
//					.parse(i.user_headimg)));
//		}

		if (!"".equals(SPUtils.getString(GroupConversationInfoActivity.this, "clickGroupicon"))) {

			Picasso.with(GroupConversationInfoActivity.this)
					.load(SPUtils.getString(GroupConversationInfoActivity.this, "clickGroupicon")).fit()
					.into(imageView2);
		}
		
		lv_group_list.setAdapter(new MyAdapter());
			}
		}, 10);
	}
	
	@Override
	public void onFailure(Request arg0, IOException arg1) {
		// TODO Auto-generated method stub
		
	}
};

	class MyAdapter extends BaseAdapter implements StickyListHeadersAdapter {

		private String substring;
		private String substringrole;

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return list.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder vh = null;
			if (convertView == null) {
				convertView = View.inflate(parent.getContext(),
						R.layout.item_conversationlist, null);
				vh = new ViewHolder();
				vh.friend_name = (TextView) convertView
						.findViewById(R.id.tv_friend_name);
				vh.friend = (TextView) convertView.findViewById(R.id.tv_friend);
				vh.iv_friend_headimg = (ImageView) convertView
						.findViewById(R.id.iv_friend_headimg);
				vh.tv_teacher = (TextView) convertView
						.findViewById(R.id.tv_teacher);
				// vh.iv_grammar_second_bg = (ImageView)
				// convertView.findViewById(R.id.iv_grammar_second_bg);
				convertView.setTag(vh);
			} else {
				vh = (ViewHolder) convertView.getTag();
			}

			vh.friend_name.setText(list.get(position).NAME);
			vh.friend.setText(list.get(position).USER_ID);
			vh.tv_teacher.setVisibility(View.INVISIBLE);
			if ("2".equals(list.get(position).ROLE)) {
				vh.tv_teacher.setVisibility(View.VISIBLE);
				vh.tv_teacher.setText("Teacher");
			} else if ("0".equals(list.get(position).ROLE)) {
				vh.tv_teacher.setVisibility(View.VISIBLE);
				vh.tv_teacher.setText("Student");
			}else if ("1".equals(list.get(position).ROLE)) {
				vh.tv_teacher.setVisibility(View.VISIBLE);
				vh.tv_teacher.setText("Parent");
			}else if ("3".equals(list.get(position).ROLE)) {
				vh.tv_teacher.setVisibility(View.VISIBLE);
				vh.tv_teacher.setText("Master");
			}

			if ("".equals(list.get(position).HEADIMG)) {
				Picasso.with(GroupConversationInfoActivity.this)
						.load(R.drawable.xuesheng).fit()
						.into(vh.iv_friend_headimg);

			} else {

				Picasso.with(GroupConversationInfoActivity.this)
						.load(list.get(position).HEADIMG).fit()
						.into(vh.iv_friend_headimg);
			}
			return convertView;
		}

		class ViewHolder {
			private TextView friend_name;
			private TextView friend;
			private TextView tv_teacher;
			private ImageView iv_friend_headimg;
		}

		@Override
		public View getHeaderView(int position, View convertView,
				ViewGroup parent) {
			TextView tv = null;
			if (convertView == null || !(convertView instanceof TextView)) {
				tv = new TextView(GroupConversationInfoActivity.this);
			} else {
				tv = (TextView) convertView;
			}
			
			substring =SPUtils.getString(GroupConversationInfoActivity.this, "clickGroupid").substring(0, 11);
			substringrole =SPUtils.getString(GroupConversationInfoActivity.this, "clickGroupid").substring(11, 12);
			if (substring.equals(list.get(position).USER_ID)&&substringrole.equals(list.get(position).ROLE)) {
				tv.setText("群主");
			} else {
				tv.setText("群成员");
			}
			tv.setTextSize(14);
			tv.setPadding(10, 0, 10, 0);
			tv.setTextColor(0xcc000000);
			tv.setBackgroundColor(0xffE1E1E1);
			return tv;
		}

		@Override
		public long getHeaderId(int position) {

			if (substring.equals(list.get(position).USER_ID)&&substringrole.equals(list.get(position).ROLE)) {
				return 0;
			} else {
				return 1;
			}

		}

	}
}
